package com.chixigua.chaos.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.Random;

public class Common {


    public static Object getObjDefault(Object obj) {
        // 得到类对象
        Class objCla = obj.getClass();
        Field[] fs = objCla.getDeclaredFields();
        for (int i = 0; i < fs.length; i++) {
            Field f = fs[i];

            // 设置些属性是可以访问的
            boolean isStatic = Modifier.isStatic(f.getModifiers());
            if (isStatic) {
                continue;
            }
            // 设置些属性是可以访问的
            f.setAccessible(true);
            try {
                // 得到此属性的值
                Object val = f.get(obj);
                // 得到此属性的类型
                String type = f.getType().toString();
                if (type.endsWith("String") && val == null) {
                    // 给属性设值
                    f.set(obj, "");
                } else if ((type.endsWith("int") || type.endsWith("Integer") || type.endsWith("double")) && val == null) {
                    f.set(obj, 0);
                } else if ((type.endsWith("long") || type.endsWith("Long")) && val == null) {
                    f.set(obj, 0L);
                } else if (type.endsWith("Date") && val == null) {
                    f.set(obj, Date.valueOf("1970-01-01"));
                } else if (type.endsWith("Timestamp") && val == null) {
                    f.set(obj, Timestamp.valueOf("1970-01-01 00:00:00"));
                } else if (type.endsWith("BigDecimal") && val == null) {
                    f.set(obj, new BigDecimal(0));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return obj;
    }

    //随机数字
    public static String newCode(int length) {
        int random = (int) (Math.random() * 1000000);
        String str = random + "";
        while (str.length() < length) {
            str = 0 + str;
        }

        return str;
    }

    /**
     * 生成指定位数的大小写字母加数字的随机数
     *
     * @param length
     * @return
     */
    public static String RamdomLetter(int length) {
        try {
            if (length <= 0) {
                return "";
            }
            char[] letters = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
                    'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                    'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'r'
            };
            int limit = letters.length;
            String result = "";
            Random random = new Random();
            for (int i = 0; i < length; i++) {
                result += letters[random.nextInt(limit)];
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }



}
